package zli.proxy.dynamicproxy.cglibproxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import zli.proxy.dynamicproxy.jdkproxy.IPerson;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Desc
 * @Author Zerlinda
 * @Date 2022/5/13 14:26
 * @Version 1.0
 */
public class CglibMeiPo implements MethodInterceptor {

    public Object getInstance(Class<?> clazz) {
        // 相当于Proxy, 代理的工具类
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    private void after() {
        System.out.println("after: 开始交往");
    }

    private void before() {
        System.out.println("before: 我是地狱少女，已经收到你的需求开始物色对象");
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        before();
        Object result = methodProxy.invokeSuper(o, objects);
        after();
        return result;

    }
}
